package com.cgbl.cg.vm.home

import android.app.Application
import android.os.Bundle
import api.ResourceServiceFactory
import api.UserServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.*
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class HomeResourceViewModel(application: Application):GLBaseViewModel(application) {

    val uc = UiChangeEvent()
    class UiChangeEvent {
        val ribbonDataEvent = SingleLiveEvent<List<BannerBean>>()
        val recommendDataEvent = SingleLiveEvent<List<ResourceBean>>()
        val popularMaterialDataEvent = SingleLiveEvent<List<ResourceBean>>()
        val limitedDataEvent = SingleLiveEvent<List<PreferentPageBean>>()
    }

    val toExcellentResourceCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityWithBundle(
            AppConstants.Router.RESOURCE.A_EXCELLENT_RESOURCE,
            Bundle().apply {
                putInt(AppConstants.BundleKey.TYPE, 0)
            }
        )
    })

    val toVipResourceCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityWithBundle(
            AppConstants.Router.RESOURCE.A_EXCELLENT_RESOURCE,
            Bundle().apply {
                putInt(AppConstants.BundleKey.TYPE, 1)
            }
        )
    })

    val toFreeResourceCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivityWithBundle(
            AppConstants.Router.RESOURCE.A_EXCELLENT_RESOURCE,
            Bundle().apply {
                putInt(AppConstants.BundleKey.TYPE, 2)
            }
        )
    })

    val toFilterResourceCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.RESOURCE.A_FILTER_RESOURCE)
    })

    val toResourceMenuCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.RESOURCE.A_RESOURCE_MENU)
    })

    fun refresh() {
        getRibbonList()
        homeData()
        getPreferentPage()
    }

    fun getRibbonList(){
        UserServiceFactory.getBannerList(
            2
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<List<BannerBean>>>(this){
            override fun onNext(t: BaseResponse<List<BannerBean>>) {
                uc.ribbonDataEvent.postValue(t.data)
            }
        })
    }

    fun homeData(){
        ResourceServiceFactory.homeData()
            .subscribe(object :AbstractViewModelSubscriber<BaseResponse<ResourceHomeBean>>(this){
                override fun onNext(t: BaseResponse<ResourceHomeBean>) {
                    uc.recommendDataEvent.postValue(t.data.hotList)
                    uc.popularMaterialDataEvent.postValue(t.data.typeList)
                }
            })
    }

    fun getPreferentPage() {
        UserServiceFactory.getPreferentPage(
            mapOf(
                "pageNo" to 1,
                "pageSize" to 10,
                "sort" to
                    "0",
                "projectType" to 1
            )
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<PreferentPageBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<PreferentPageBean>>) {
                uc.limitedDataEvent.postValue(t.data.list)

            }
        })
    }
}